We already mentioned that the actual type of a polymorphic object can be changed by assignment during runtime. To allow assignment to polymorphic objects, some compatibility rules have to be fulfilled.

Have a look at the following declarations with the class type definitions from the previous page:

VARIABLE pack1 : packet'CLASS;
                  -- class-wide type

VARIABLE pack2 : special_packet_1;
                  -- derived from packet

VARIABLE pack3 : special_packet_2;
                  -- derived from packet

and the following assignments:


pack1 := pack2;
This assignment is correct because pack2 is of a class type derived from packet and pack1 is of a class-wide type. The assignment changes the actual type of pack1 to special_packet_1.

pack2 := pack3;
This is an error because the types of pack2 and pack3 are incompatible. This can be checked during compilation.

pack2 := pack1;
Whether this assignment is correct, depends on the actual type of pack1. In case it is special_packet_1 it is correct, otherwise it's an error. This is a typical runtime error and can't be detected during compilation.